package com.press.filters;


import com.press.handlers.UrlHandler;
import com.press.handlers.UrlHandlerAbs;
import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//import org.apache.log4j.Logger;

/**
 * Created by haocongshun on 2016/10/1.
 */
public class UrlFilter implements Filter {
    private static Logger log;
    UrlHandlerAbs urlHandler;
    public void init(FilterConfig filterConfig) throws ServletException {
        urlHandler = new UrlHandler();
        log= Logger.getLogger(UrlFilter.class);
    }

    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)servletRequest;
        HttpServletResponse response = (HttpServletResponse)servletResponse;
        String url = request.getRequestURI();
        Boolean[] isDohandle = new Boolean[]{false};
        try{
            urlHandler.dohandle(url,request,response,isDohandle);
        }catch (Exception e){
            log.error("UrlFilter处理出错！");
            e.printStackTrace();
        }
        if(!isDohandle[0]){
            filterChain.doFilter(request,response);
        }

    }

    public void destroy() {

    }
}
